package com.zzyl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description DeviceDataServiceImpl
 * @Author songyu
 * @Date 2024-11-02
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 物模型分页数据查询
     *
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(),
                deviceDataPageReqDto.getPageSize());
        //将deviceDataPageReqDto中Long类型的startTime和endTime转换为LocalDateTime
        LocalDateTime startDate = LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime());
        LocalDateTime endDate = LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime());
        //封装给deviceDataPageReqDto
        deviceDataPageReqDto.setStartDate(startDate);
        deviceDataPageReqDto.setEndDate(endDate);
        //执行数据库查询
        Page<DeviceDataVo> page = deviceDataMapper.findListByCondition(deviceDataPageReqDto);
        //封装返回
        return PageResponse.of(page,DeviceDataVo.class);
    }

    /**
     * 取按日查询设备数据
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        
        //1.调用mapper按日查询设备数据
        List<DeviceDataGraphVo> deviceDataGraphVoList = deviceDataMapper.queryDeviceDataListByDay(
                iotId,functionId,LocalDateTimeUtil.of(startTime),LocalDateTimeUtil.of(endTime)
        );
        //将deviceDataGraphVoList转换为map,将里面每个DeviceDataGraphVo的dateTime作为key,dataValue作为value
        Map<String,Double> map = null;
        if(CollectionUtil.isNotEmpty(deviceDataGraphVoList)){
            map = deviceDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime,
                    DeviceDataGraphVo::getDataValue));
        }
        
        //2.构建24小时图形列表集合
        List<DeviceDataGraphVo> deviceDataGraphVo24List = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));

        //3.遍历deviceDataGraphVo24List集合，填充每个小时数据
        for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVo24List) {
            Double value = map.get(deviceDataGraphVo.getDateTime());
            if(value!=null) {
                deviceDataGraphVo.setDataValue(value);
            }
        }
        return deviceDataGraphVo24List;
    }

    /**
     * 取按周查询设备数据
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        //1.调用mapper按周查询设备数据
        List<DeviceDataGraphVo> deviceDataGraphVoList = deviceDataMapper.queryDeviceDataListByWeek(
                iotId,functionId,LocalDateTimeUtil.of(startTime),LocalDateTimeUtil.of(endTime)
        );
        //将deviceDataGraphVoList转换为map,将里面每个DeviceDataGraphVo的dateTime作为key,dataValue作为value
        Map<String,Double> map = null;
        if(CollectionUtil.isNotEmpty(deviceDataGraphVoList)){
            map = deviceDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime,
                    DeviceDataGraphVo::getDataValue));
        }

        //2.构建最近7天图形列表集合
        List<DeviceDataGraphVo> deviceDataGraphVo7DayList = DeviceDataGraphVo.weekInstance(LocalDateTimeUtil.of(startTime));

        //3.遍历deviceDataGraphVo7DayList集合，填充每天数据
        for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVo7DayList) {
            Double value = map.get(deviceDataGraphVo.getDateTime());
            if(value!=null) {
                deviceDataGraphVo.setDataValue(value);
            }
        }
        return deviceDataGraphVo7DayList;
    }
}
